package org.kinglone.common.interceptor;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.kinglone.back.article.model.Article;
import org.kinglone.back.article.service.ArticleService;
import org.kinglone.back.classify.model.Classify;
import org.kinglone.back.classify.service.ClassifyService;
import org.kinglone.back.comment.model.Comment;
import org.kinglone.back.comment.service.CommentService;
import org.kinglone.back.friendlinks.model.Friendlinks;
import org.kinglone.back.friendlinks.service.FriendlinksService;
import org.kinglone.back.music.model.Music;
import org.kinglone.back.music.service.MusicService;
import org.kinglone.back.system.model.SystemParam;
import org.kinglone.back.system.service.SystemParamService;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
/**
 * 
 * @author 
 *
 */
public class MeauInterceptor extends HandlerInterceptorAdapter {
	
	   @Resource(name="musicService")
	   private MusicService musicService;

	   @Resource(name="friendlinksService")
	   private FriendlinksService friendlinksService;
	   
	   @Resource(name="classifyService")
	   private ClassifyService classifyService;
	   
	   @Resource(name="articleService")
	   private ArticleService articleService;
	   
	   @Resource(name="commentService")
	   private CommentService commentService;
	   
	   @Resource(name="systemParamService")
	   private SystemParamService systemParamService;
	   
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		String url = request.getRequestURL().toString();
        JSONArray jsonArray = new JSONArray();
        List<Music> list = musicService.getAll();
        for (int i = 0;i<list.size();i++){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("url","/blog"+list.get(i).getMusicUrl());
            jsonObject.put("name",list.get(i).getMusicName());
            jsonArray.add(jsonObject);
        }
        request.setAttribute("MusicData",jsonArray);
        
        List<Friendlinks> friendlinks = friendlinksService.getAll();
        request.setAttribute("friendlinks",friendlinks);
        
        List<Classify> classifies = classifyService.getAll();
        List<Map<String, Object>> listMap = new ArrayList<Map<String,Object>>();
        for(int i=0;i<classifies.size();i++) {
        	Map<String,Object> map = new HashMap<String, Object>();
        	BigInteger articleCount = articleService.getArticleCount(classifies.get(i).getSysid());
            map.put("classifyId", classifies.get(i).getSysid());
            map.put("classifyName", classifies.get(i).getClassifyName());
            map.put("articleCount", articleCount);
            listMap.add(map);
        }
        request.setAttribute("listMap",listMap);

        List<Article> articles = articleService.getArticleByNum();
        request.setAttribute("articles",articles);
        
        List<Comment> comments = commentService.getCommentByNum();
        List<Map<String, Object>> commentListMap = new ArrayList<Map<String,Object>>();
        for(int i=0;i<comments.size();i++) {
            Map<String,Object> map = new HashMap<String, Object>();
            String commentId = comments.get(i).getSysid();
            Comment comment = commentService.get(commentId);
            Article article = articleService.get(comment.getArticle().getSysid());
            map.put("articleId", article.getSysid());
            map.put("commentContent", comments.get(i).getContent());
            map.put("articleTitle", article.getTitle());
            map.put("commentId",commentId);
            commentListMap.add(map);
        }
        request.setAttribute("commentListMap",commentListMap);
        
       SystemParam copyright = systemParamService.get("copyright");
       request.setAttribute("copyright", copyright);
       
       SystemParam author = systemParamService.get("author");
       request.setAttribute("author", author);

        SystemParam keywords = systemParamService.get("keywords");
        request.setAttribute("keywords", keywords);

        SystemParam description = systemParamService.get("description");
        request.setAttribute("description", description);
       
       SystemParam leftFooter = systemParamService.get("leftFooter");
       request.setAttribute("leftFooter", leftFooter);
       
       SystemParam logion = systemParamService.get("logion");
       request.setAttribute("logion", logion);
       
       SystemParam rightFooter = systemParamService.get("rightFooter");
       request.setAttribute("rightFooter", rightFooter);
       
       SystemParam statement = systemParamService.get("statement");
       request.setAttribute("statement", statement);
        

		
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
	}


}
